<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form size="small" :inline="true">
            <el-form-item label="回复类型：">
              <el-select v-model="tableFrom.type" placeholder="请选择类型" class="selWidth" clearable @change="seachList">
                <el-option label="文本消息" value="text"/>
                <el-option label="图片消息" value="image" disabled/>
                <el-option label="图文消息" value="news" disabled/>
                <el-option label="音频消息" value="voice" disabled/>
              </el-select>
            </el-form-item>
            <el-form-item label="关键字：">
              <el-input v-model="tableFrom.keywords" placeholder="请输入关键字" class="selWidth" size="small" clearable>
                <el-button slot="append" icon="el-icon-search" size="small" @click="seachList"/>
              </el-input>
            </el-form-item>
          </el-form>
          <router-link :to="{path: '/wechat/gzh/reply/keyword/save'}">
            <el-button
              size="small"
              type="primary"
            >添加关键字
            </el-button>
          </router-link>
        </div>
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="small"
        highlight-current-row
      >
        <el-table-column
          prop="id"
          label="ID"
          width="60"
        />
        <el-table-column
          prop="keywords"
          label="关键字"
          min-width="150"
        />
        <el-table-column
          label="回复类型"
          min-width="100"
        >
          <template slot-scope="scope">
            <span>{{ keywordStatusFilter(scope.row.type) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="status"
          :label="$t('hidden')"
          min-width="100"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="true"
              :inactive-value="false"
              :active-text="$t('show')"
              :inactive-text="$t('hidden')"
              @change="onchangeIsShow(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column :label="$t('function')" min-width="100">
          <template slot-scope="scope">
            <router-link :to="{path: '/wechat/gzh/reply/keyword/save/' + scope.row.id}">
              <el-button type="text" size="small">{{ $t('edit') }}</el-button>
            </router-link>
            <el-button type="text" size="small" @click="handleDelete(scope.row.id, scope.$index)">{{ $t('delete') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin: 15px 0;text-align: right">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.pageSize"
          :current-page="tableFrom.currentPage"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { pageGetReplyApi, replyDeleteApi, replyStatusApi } from '@/api/iot/iotWeChatGzhReplyApi'
import { getCompanyId } from '@/util/storage'
import { cancelNotice, successNotice } from '@/util/common'

export default {
  name: 'ReplyKeyword',
  data() {
    return {
      tableData: {
        data: [],
        total: 0
      },
      tableFrom: {
        companyId: getCompanyId(),
        currentPage: 1,
        pageSize: 20,
        keywords: '',
        type: ''
      },
      listLoading: true
    }
  },
  created() {
    this.getList()
  },
  methods: {
    keywordStatusFilter(status) {
      const statusMap = {
        'text': '文字消息',
        'image': '图片消息',
        'news': '图文消息',
        'voice': '声音消息'
      }
      return statusMap[status]
    },
    seachList() {
      this.tableFrom.currentPage = 1
      this.getList()
    },
    onchangeIsShow(row) {
      replyStatusApi({
        id: row.id,
        status: row.status
      }).then(() => {
        successNotice(this)
        this.getList()
      }).catch(() => {
        row.status = !row.status
      })
    },
    // 列表
    getList() {
      this.listLoading = true
      pageGetReplyApi(
        this.tableFrom
      ).then(res => {
        const { data, code } = res
        if (code === '200') {
          this.tableData.data = data.data
          this.tableData.total = data.total
        }
        this.listLoading = false
      })
    },
    pageChange(currentPage) {
      this.tableFrom.currentPage = currentPage
      this.getList()
    },
    handleSizeChange(val) {
      this.tableFrom.pageSize = val
      this.getList()
    },
    // 删除
    handleDelete(id, idx) {
      const that = this
      this.$confirm(this.$t('deletePrompt'), this.$t('prompt'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      })
        .then(function() {
          replyDeleteApi({
            id: id
          }).then(() => {
            successNotice(this)
            that.getList()
          })
        })
        .catch(function() {
          cancelNotice(that)
        })
    }
  }
}
</script>


